//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    uindex: null,
    imgUrls: [
      {
        bannerImg: "../../image/pet/pet_banner_1.jpg"
      },
      {
        bannerImg: "../../image/pet/pet_banner_2.jpg"
      },
      {
        bannerImg: "../../image/pet/pet_banner_3.jpg"
      }
    ],
    circular: true,
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 5000,
    duration: 1500,
    petList: [],
    page: 1,
    length: 1,
    petList: [],
    petType : 1,
    petIcon: 'bone'
  },
  //事件处理函数
  petDetail: function(e){
    var petId = e.currentTarget.dataset.petId;
    wx.navigateTo({
      url: '/pages/detail/detail?petId='+petId
    })
  },
  queryCenterPets: function(e){
    let that = this;
    let petType = e.currentTarget.dataset.petType;
    let _petIcon = '';
    if(petType == '1'){
      _petIcon = 'bone';
    }else if(petType = '2'){
      _petIcon = 'fish';
    }else{
      _petIcon = 'other';
    }
    that.setData({
      petType: petType,
      petList: [], 
      petIcon: _petIcon
    });
    this.queryPetList();
  },
  queryPetList: function(){
    let that = this;
    let data = 'petType='+that.data.petType+ '&pageNum='+ that.data.page+'&pageSize=10'
    wx.showToast({
      title: '加载中！',
      icon: 'loading',
      duration: 1000
    });
    let fn = res => {
      let _list = that.data.petList;
      for (var i = 0; i < res.data.data.length; i++) {
        _list.push(res.data.data[i]);
      }
      if (that.data.page == 1) {
        _list = res.data.data;
      }
      that.setData({
        petList: _list,
        page: _list.length >= 10 ? 1: Math.floor(_list.length/10)+1,
        length: res.data.data.length
      })
    }
    app.ajax('/petAdopt/list?'+data, 'get', '', fn)
  },
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    //模拟加载
    this.queryPetList();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  //加载更多
  onReachBottom: function() {
    let that = this;
    if (that.data.length < 10) {
      wx.showToast({
        title: '没有更多了',
        icon: 'none'
      })
      return false;
    } else {
      wx.showToast({
        title: '加载中！',
        icon: 'loading',
        duration: 1000
      });
    }
    this.queryPetList();
  },
  onLoad: function () {
    let that = this;
    //调用应用实例的方法获取全局数据
    wx.showLoading({
      title: '加载中',
    });
    app.appLogin(0);  
    that.queryPetList();
  },
  onShow:function(){
    // 设置app语言的全局变量  
    var selectedLanguage = app.globalData.settings.language;
    this.setData({
      uindex: selectedLanguage    
    })
  },
  bindchange: function (e) {
    // console.log(e.detail.current)
    this.setData({ current: e.detail.current })
  },
  toAdoptList: function(){
    wx.navigateTo({
      url: '/pages/petList/adopt/adopt'
    })
  },
  toLostList: function(e){
    var releaseType = e.currentTarget.dataset.releaseType;
    wx.navigateTo({
      url: '/pages/petList/lost/lost?releaseType=' + releaseType
    })
  }
})
